package cn.fl.repay.controller;

import cmm.mid.core.framework.page.DataResultVO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.ListPageVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseConstant;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.project.condition.PrjPrdPaySchMQueryCondition;
import cn.fl.project.service.IPrjPrdPaySchMService;
import cn.fl.repay.service.IInsCstRepayLogService;
import cn.fl.repay.service.IInsCstRepaySettleService;
import cn.fl.repay.vo.InsCstRepaySettleVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import pam.transmissionrec.facade.condition.InfTransLogExtQueryCondition;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/repay/insCstRepaySettle")
public class InsCstRepaySettleController {

    private static final Logger logger = LoggerFactory.getLogger(InsCstRepaySettleController.class);

    @Autowired
    private IInsCstRepaySettleService insCstRepaySettleService;
    @Autowired
    private IInsCstRepayLogService insCstRepayLogService;
    @Autowired
    private IPrjPrdPaySchMService prjPrdPaySchMService;


    /**
     * 到代偿列表页面
     */
    @RequestMapping(value = "/toListPage.do", method = {RequestMethod.GET})
    public ModelAndView toListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toListPage", "加载InsCstRepaySettle列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("repay/insCstRepaySettle/insCstRepaySettleList");

        //资管代偿支持的机构 CAP0073
        List<KeyValueVO> insCdList = this.insCstRepayLogService.getKVList("CAP0073");
        mav.addObject("insCdList", JSON.toJSONString(insCdList));
        return mav;
    }

    /**
     * 到代偿记录列表页面
     */
    @RequestMapping(value = "/toRepayLogListPage.do", method = {RequestMethod.GET})
    public ModelAndView toRepayLogListPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toRepayLogListPage", "加载toRepayLogListPage列表页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView("repay/insCstRepaySettle/insCstRepayLogList");

        //资管代偿支持的机构 CAP0073
        List<KeyValueVO> insCdList = this.insCstRepayLogService.getKVList("CAP0073");
        mav.addObject("insCdList", JSON.toJSONString(insCdList));
        return mav;
    }


    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initInsCstRepaySettleList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }
        PrjPrdPaySchMQueryCondition qc = new PrjPrdPaySchMQueryCondition();
        String paySchNo = request.getParameter("paySchNo");
        if (StringUtils.isNotEmpty(paySchNo)) {
            qc.setPaySchNo(paySchNo);
        }
        String paySchTypCd = request.getParameter("paySchTypCd");
        if (StringUtils.isNotEmpty(paySchTypCd)) {
            qc.setPaySchTypCd(paySchTypCd);
        }
        String isEnable = request.getParameter("isEnable");
        if (StringUtils.isNotEmpty(isEnable)) {
            qc.setIsEnable(Integer.parseInt(isEnable));
        }
        String insCd = request.getParameter("insCode");
        if (StringUtils.isNotEmpty(insCd)) {
            qc.setBizFlag(insCd);
        } else {
            List<KeyValueVO> insCdList = this.insCstRepayLogService.getKVList("CAP0073");
            StringBuffer insCodes = new StringBuffer("'-1'");
            for (KeyValueVO keyValueVO : insCdList) {
                insCodes.append(",");
                insCodes.append("'" + keyValueVO.getKey() + "'");
            }
            qc.setBizFlags(insCodes.toString());
        }
        qc.setIsStartRent(1);
        page.setCondition(qc);
        BaseLogUtils.info(logger, "initInsCstRepaySettleList", "初始化initInsCstRepaySettleList列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        Pager pager = this.insCstRepaySettleService.searchListPage(page);
        List<InsCstRepaySettleVO> list = (List<InsCstRepaySettleVO>) pager.getResultList();
        ListPageVO<InsCstRepaySettleVO> listPage = new ListPageVO<InsCstRepaySettleVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 到代偿或结清页面
     */
    @RequestMapping(value = "/toAddPage.do", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView toAddPage(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "toAddPage", "加载insCstRepaySettle Add页面.", CurrentThreadContext.getCurrentUserName());
        ModelAndView mav = new ModelAndView();
        InsCstRepaySettleVO vo = new InsCstRepaySettleVO();
        String prjId = request.getParameter("prjId");
        vo.setPrjId(Long.parseLong(prjId));
        String insId = request.getParameter("insId");
        vo.setInsId(Long.parseLong(insId));
        String paySchNo = request.getParameter("paySchNo");
        vo.setPaySchNo(paySchNo);
        String insCode = request.getParameter("insCode");
        vo.setInsCode(insCode);
        String id = request.getParameter("id");
        vo.setId(Long.parseLong(id));
        mav.addObject("insCstRepaySettleVO", vo);
        //repay：代偿 settle：结清
        String type = request.getParameter("type");
        DataResultVO resultVO = null;
        //是否有异常 0.否 1.是
        mav.addObject("errorFlag", 1);
        //还款资金来源
        List<KeyValueVO> accTypCdList = this.insCstRepayLogService.getKVList("CAP0074");
        mav.addObject("accTypCdList", JSON.toJSONString(accTypCdList));

        if ("repay".equals(type)) {
            if ("ybccbank".equals(insCode)) {
                mav.setViewName("repay/insCstRepaySettle/insCstRepayNoCalc");
            } else if("zbbank".equals(insCode)) {
                mav.setViewName("repay/insCstRepaySettle/insCstRepayZbbank");
            }else {
                mav.setViewName("repay/insCstRepaySettle/insCstRepay");
            }
            resultVO = this.insCstRepaySettleService.searchRepayInfo(vo);
            BaseLogUtils.newLogger("加载代偿还款页面.").setKeys(JSON.toJSONString(resultVO)).info();
            if (resultVO.getSuccess() == 1) {
                mav.addObject("repayList", resultVO.getData());
                //是否有异常 0.否 1.是
                mav.addObject("errorFlag", 0);
                //欠款总金额
                mav.addObject("ttlAmt", resultVO.getSignature());
            } else {
                mav.addObject("errorMsg", resultVO.getInfo());
            }
        } else {
            if ("ybccbank".equals(insCode)) {
                mav.setViewName("repay/insCstRepaySettle/insCstSettleNoCalc");
            } else if ("hrbank".equals(insCode)) {
                mav.setViewName("repay/insCstRepaySettle/insCstSettleHrbank");
            } else if ("ybccbank".equals(insCode)) {
                mav.setViewName("repay/insCstRepaySettle/insCstSettleYbbank");
            }else if("zbbank".equals(insCode)){
                mav.setViewName("repay/insCstRepaySettle/insCstSettleZbbank");
            }else if("tyfl".equals(insCode)){
                mav.setViewName("repay/insCstRepaySettle/insCstSettleTybank");
            }else if("spdbank".equals(insCode)){
                mav.setViewName("repay/insCstRepaySettle/insCstSettleSpdbank");
            }else{
                mav.setViewName("repay/insCstRepaySettle/insCstSettle");
            }
            if(!"zbbank".equals(insCode)) {
            resultVO = this.insCstRepaySettleService.searchSettleInfo(vo);
            }else{
                resultVO = new DataResultVO(BaseConstant.IS_YESNO_YES);
                resultVO.setData(vo);
            }
            BaseLogUtils.newLogger("加载提前结清页面.").setKeys(JSON.toJSONString(resultVO)).info();
            if (resultVO.getSuccess() == 1) {
                mav.addObject("insCstRepaySettleVO", resultVO.getData());
                //是否有异常 0.否 1.是
                mav.addObject("errorFlag", 0);
            } else {
                mav.addObject("errorMsg", resultVO.getInfo());
            }
        }
        return mav;
    }

    /**
     * 提前还款或结清
     */
    @RequestMapping(value = "/repayOrsettle.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object settle(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "settle", ".", CurrentThreadContext.getCurrentUserName());
        InsCstRepaySettleVO vo = new InsCstRepaySettleVO();
        String priId = request.getParameter("prjId");
        vo.setPrjId(Long.parseLong(priId));
        String insId = request.getParameter("insId");
        vo.setInsId(Long.parseLong(insId));
        String id = request.getParameter("id");
        vo.setId(Long.parseLong(id));
        String accTypCd = request.getParameter("accTypCd");
        String payPd = request.getParameter("payPd");
        String repayTtlAmt = request.getParameter("repayTtlAmt");
        if(StringUtils.isNotBlank(payPd)){
            vo.setPayPd(Integer.valueOf(payPd));
        }
        if(StringUtils.isNotBlank(repayTtlAmt)){
            vo.setRepayTtlAmt(repayTtlAmt);
        }
        vo.setAccTypCd(accTypCd);
        String insCode = request.getParameter("insCode");
        vo.setInsCode(insCode);
        String repayTotalAmt = request.getParameter("repayTtlAmt");
        if (StringUtils.isNotEmpty(repayTotalAmt)) {
            vo.setRepayTtlAmt(repayTotalAmt);
        } else {
            vo.setRepayTtlAmt("0");
        }
        String repayCapAmt = request.getParameter("repayCapAmt");
        if (StringUtils.isNotEmpty(repayCapAmt)) {
            vo.setRepayCapAmt(repayCapAmt);
        }

        //repay：代偿 settle：结清
        String type = request.getParameter("type");
        DataResultVO resultVO = null;
        if ("repay".equals(type)) {
            resultVO = this.insCstRepaySettleService.repay(vo);
        } else {
            resultVO = this.insCstRepaySettleService.settle(vo);
        }
        BaseLogUtils.newLogger("提前还款或结清返回：").setKeys(JSON.toJSONString(resultVO)).info();
        return resultVO;
    }


    /**
     * 初始化列表
     */
    @SuppressWarnings("unchecked")
    @RequestMapping(value = "/initRepayLogList.do", method = {RequestMethod.GET})
    @ResponseBody
    public Object initRepayLogList(HttpServletRequest request, HttpServletResponse response) {
        String currentPage = request.getParameter("page");
        String pageSize = request.getParameter("pagesize");
        Pager page = new Pager();
        if (StringUtils.isNotEmpty(currentPage)) {
            page.setCurrentPage(Integer.parseInt(currentPage));
        }
        if (StringUtils.isNotEmpty(pageSize)) {
            page.setEveryPage(Integer.parseInt(pageSize));
        }

        InfTransLogExtQueryCondition qc = new InfTransLogExtQueryCondition();
        String action = request.getParameter("action");
        if (StringUtils.isNotEmpty(action)) {
            qc.setAction(action);
        } else {
            qc.setAction("platformCompensatory");
        }

        String paySchNo = request.getParameter("paySchNo");
        if (StringUtils.isNotEmpty(paySchNo)) {
            qc.setPaySchNo(paySchNo);
        }
        String insCd = request.getParameter("insCode");
        if (StringUtils.isNotBlank(insCd)) {
            qc.setFundRasInsNo(insCd);
        }
        page.setCondition(qc);
        Pager pager = this.insCstRepaySettleService.searchRepayLogListPage(page);
        BaseLogUtils.info(logger, "initRepayLogList", "初始化initRepayLogList列表页面数据.DATA:" + JSONObject.toJSONString(page), CurrentThreadContext.getCurrentUserName());
        List<InsCstRepaySettleVO> list = (List<InsCstRepaySettleVO>) pager.getResultList();
        ListPageVO<InsCstRepaySettleVO> listPage = new ListPageVO<InsCstRepaySettleVO>(list, pager.getTotalCount());
        return listPage;
    }

    /**
     * 提前还款或结清
     */
    @RequestMapping(value = "/settleCal.do", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public Object settleCal(HttpServletRequest request, HttpServletResponse response) {
        BaseLogUtils.info(logger, "settle", ".", CurrentThreadContext.getCurrentUserName());
        InsCstRepaySettleVO vo = new InsCstRepaySettleVO();
        String priId = request.getParameter("prjId");
        vo.setPrjId(Long.parseLong(priId));
        String insId = request.getParameter("insId");
        vo.setInsId(Long.parseLong(insId));
        String id = request.getParameter("id");
        vo.setId(Long.parseLong(id));
        String accTypCd = request.getParameter("accTypCd");
       // String payPd = request.getParameter("payPd");
        String repayTtlAmt = request.getParameter("repayTtlAmt");
      /*  if(StringUtils.isNotBlank(payPd)){
            vo.setPayPd(Integer.valueOf(payPd));
        }*/
        if(StringUtils.isNotBlank(repayTtlAmt)){
            vo.setRepayTtlAmt(repayTtlAmt);
        }
        vo.setAccTypCd(accTypCd);
        String insCode = request.getParameter("insCode");
        vo.setInsCode(insCode);
        String repayTotalAmt = request.getParameter("repayTtlAmt");
        if (StringUtils.isNotEmpty(repayTotalAmt)) {
            vo.setRepayTtlAmt(repayTotalAmt);
        } else {
            vo.setRepayTtlAmt("0");
        }
        String type = request.getParameter("type");
        DataResultVO resultVO = null;
            resultVO =  this.insCstRepaySettleService.searchSettleInfo(vo);
        BaseLogUtils.newLogger("提前结清试算返回：").setKeys(JSON.toJSONString(resultVO)).info();
        return resultVO;
    }

}
